﻿using DSPL.Contracts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    /// <summary>
    /// Common Textbox tool for handeling Tax Details
    /// </summary>
    public partial class TxtTax : UserControl
    {
        public TxtTax()
        {
            InitializeComponent();
        }

        #region Declaring Properties

        #region General Properties

        public HelpNames HelpName
        {
            get { return (HelpNames)GetValue(HelpNameProperty); }
            set { SetValue(HelpNameProperty, value); }
        }
        
        public object TaxKeyValue
        {
            get { return (object)GetValue(TaxKeyValueProperty); }
            set { SetValue(TaxKeyValueProperty, value); }
        }

        public string KeyFieldName
        {
            get { return (string)GetValue(KeyFieldNameProperty); }
            set { SetValue(KeyFieldNameProperty, value); }
        }

        public string KeyFieldType
        {
            get { return (string)GetValue(KeyFieldTypeProperty); }
            set { SetValue(KeyFieldTypeProperty, value); }
        }

        public DataRow SelectedDataRow
        {
            get { return (DataRow)GetValue(SelectedDataRowProperty); }
            set { SetValue(SelectedDataRowProperty, value); }
        }

        public Modes ParentFormMode
        {
            get { return (Modes)GetValue(ParentFormModeProperty); }
            set { SetValue(ParentFormModeProperty, value); }
        }

        public bool IsMandatory
        {
            get { return (bool)GetValue(IsMandatoryProperty); }
            set { SetValue(IsMandatoryProperty, value); }
        }

        #endregion

        #region txtCode Properties

        public Visibility CodeVisibility
        {
            get { return (Visibility)GetValue(CodeVisibilityProperty); }
            set { SetValue(CodeVisibilityProperty, value); }
        }

        public bool CodeEnable
        {
            get { return (bool)GetValue(CodeEnableProperty); }
            set { SetValue(CodeEnableProperty, value); }
        }

        public string TaxCode
        {
            get { return (string)GetValue(TaxCodeProperty); }
            set { SetValue(TaxCodeProperty, value); }
        }

        public int CodeColumnSpan
        {
            get { return (int)GetValue(CodeColumnSpanProperty); }
            set { SetValue(CodeColumnSpanProperty, value); }
        }

        public string TaxCodeFieldName
        {
            get { return (string)GetValue(TaxCodeFieldNameProperty); }
            set { SetValue(TaxCodeFieldNameProperty, value); }
        }



        #endregion

        #region txtDescription Properties

        public Visibility DescriptionVisibility
        {
            get { return (Visibility)GetValue(DescriptionVisibilityProperty); }
            set { SetValue(DescriptionVisibilityProperty, value); }
        }

        public bool DescriptionEnable
        {
            get { return (bool)GetValue(DescriptionEnableProperty); }
            set { SetValue(DescriptionEnableProperty, value); }
        }

        public string TaxDescription
        {
            get { return (string)GetValue(TaxDescriptionProperty); }
            set { SetValue(TaxDescriptionProperty, value); }
        }
        
        public int DescriptionColumnSpan
        {
            get { return (int)GetValue(DescriptionColumnSpanProperty); }
            set { SetValue(DescriptionColumnSpanProperty, value); }
        }

        public int DescriptionColumn
        {
            get { return (int)GetValue(DescriptionColumnProperty); }
            set { SetValue(DescriptionColumnProperty, value); }
        }

        public bool IsFocus
        {
            get { return (bool)GetValue(IsFocusProperty); }
            set { SetValue(IsFocusProperty, value); }
        }

        public string TaxDescriptionFieldName
        {
            get { return (string)GetValue(TaxDescriptionFieldNameProperty); }
            set { SetValue(TaxDescriptionFieldNameProperty, value); }
        }


        #endregion

        #region Other Tax Details Properties

        public decimal TaxPrecentage
        {
            get { return (decimal)GetValue(TaxPrecentageProperty); }
            set { SetValue(TaxPrecentageProperty, value); }
        }

        public decimal TaxRate
        {
            get { return (decimal)GetValue(TaxRateProperty); }
            set { SetValue(TaxRateProperty, value); }
        }

        #endregion

        #endregion

        #region Dependency Properties

        public static readonly DependencyProperty HelpNameProperty = DependencyProperty.Register("HelpName", typeof(HelpNames), typeof(TxtTax), new PropertyMetadata(HelpNames.TaxHelp));

        public static readonly DependencyProperty TaxKeyValueProperty = DependencyProperty.Register("TaxKeyValue", typeof(object), typeof(TxtTax), new PropertyMetadata(null));

        public static readonly DependencyProperty KeyFieldNameProperty = DependencyProperty.Register("KeyFieldName", typeof(string), typeof(TxtTax), new PropertyMetadata(""));

        public static readonly DependencyProperty KeyFieldTypeProperty = DependencyProperty.Register("KeyFieldType", typeof(string), typeof(TxtTax), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedDataRowProperty = DependencyProperty.Register("SelectedDataRow", typeof(DataRow), typeof(TxtTax), new PropertyMetadata(null));

        public static readonly DependencyProperty CodeVisibilityProperty = DependencyProperty.Register("CodeVisibility", typeof(Visibility), typeof(TxtTax), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty CodeEnableProperty = DependencyProperty.Register("CodeEnable", typeof(bool), typeof(TxtTax), new PropertyMetadata(true));

        public static readonly DependencyProperty TaxCodeProperty = DependencyProperty.Register("TaxCode", typeof(string), typeof(TxtTax), new PropertyMetadata(""));

        public static readonly DependencyProperty TaxCodeFieldNameProperty = DependencyProperty.Register("TaxCodeFieldName", typeof(string), typeof(TxtTax), new PropertyMetadata("TaxCode"));

        public static readonly DependencyProperty TaxDescriptionFieldNameProperty = DependencyProperty.Register("TaxDescriptionFieldName", typeof(string), typeof(TxtTax), new PropertyMetadata("TaxName"));

        public static readonly DependencyProperty DescriptionVisibilityProperty = DependencyProperty.Register("DescriptionVisibility", typeof(Visibility), typeof(TxtTax), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty DescriptionEnableProperty = DependencyProperty.Register("DescriptionEnable", typeof(bool), typeof(TxtTax), new PropertyMetadata(false));

        public static readonly DependencyProperty TaxDescriptionProperty = DependencyProperty.Register("TaxDescription", typeof(string), typeof(TxtTax), new PropertyMetadata(""));

        public static readonly DependencyProperty ParentFormModeProperty = DependencyProperty.Register("ParentFormMode", typeof(Modes), typeof(TxtTax), new PropertyMetadata(Modes.Undo));

        public static readonly DependencyProperty IsMandatoryProperty = DependencyProperty.Register("IsMandatory", typeof(bool), typeof(TxtTax), new PropertyMetadata(false));

        public static readonly DependencyProperty CodeColumnSpanProperty = DependencyProperty.Register("CodeColumnSpan", typeof(int), typeof(TxtTax), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnSpanProperty = DependencyProperty.Register("DescriptionColumnSpan", typeof(int), typeof(TxtTax), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnProperty = DependencyProperty.Register("DescriptionColumn", typeof(int), typeof(TxtTax), new PropertyMetadata(1));

        public static readonly DependencyProperty IsFocusProperty = DependencyProperty.Register("IsFocus", typeof(bool), typeof(TxtTax), new PropertyMetadata(false));

        public static readonly DependencyProperty TaxRateProperty = DependencyProperty.Register("TaxRate", typeof(decimal), typeof(TxtTax), new PropertyMetadata(0.0M));

        public static readonly DependencyProperty TaxPrecentageProperty = DependencyProperty.Register("TaxPrecentage", typeof(decimal), typeof(TxtTax), new PropertyMetadata(0.0M));

        #endregion

        #region Exposable Event Handlers

        public event EventHandler<EventArgs> RefreshTaxRelatedData;             //use to refresh data regarding Tax Master Text box

        #endregion

        #region Internal Events

        private void RefreshAttachedObjects(object sender, EventArgs e)
        {
            SetTaxData();

            if (RefreshTaxRelatedData != null)
                RefreshTaxRelatedData(sender, e);
        }

        //private static void SelectedDataRowChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    TxtTaxMaster objTax = d as TxtTaxMaster;
        //    if (objTax != null)
        //    {
        //        objTax.SetTaxData();
        //    }
        //}

        private void SetTaxData()
        {
            if (SelectedDataRow != null && HelpName == HelpNames.TaxHelp)
            {
                //TaxRate = (Double)SelectedDataRow["TaxRate"];
                TaxPrecentage = (decimal)SelectedDataRow["TaxPer"];
            }
            else
            {
                TaxRate = 0.0M;
                TaxPrecentage = 0.0M;
            }
        }
        #endregion
    }
}
